﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace GameBanChim_Version1
{
    public class StateMenu : GameState
    {
        Background _backGround;
        List<Button> _dsButton;

        public override void InnitState(MyGame owner)
        {
            base.InnitState(owner);
            _backGround = (Background)MyGameOwner.BackGroundManager.CreateObject(0);

            _dsButton = new List<Button>();

            _dsButton.Add((Button)MyGameOwner.ButtonManager.CreateObject(0));
            ((Button)_dsButton[0]).IsPlaySound = false; //Button newgame khong can playsound
            _dsButton.Add((Button)MyGameOwner.ButtonManager.CreateObject(1));
            _dsButton.Add((Button)MyGameOwner.ButtonManager.CreateObject(2));
            _dsButton.Add((Button)MyGameOwner.ButtonManager.CreateObject(3));
            ((Button)_dsButton[3]).IsPlaySound = false; //button exit ko can playsuond
            _dsButton.Add((Button)MyGameOwner.ButtonManager.CreateObject(6));
            for (int i = 0; i < _dsButton.Count; i++)
            {
                _dsButton[i].Sprite[0].Itexture2D = 0;
            }

        }

        public override void Update(GameTime gameTime)
        {
            _backGround.Update(gameTime);
            for (int i = 0; i < _dsButton.Count; i++)
            {
                if (_dsButton[i].isHover())
                {
                    // do nothing.
                }
                if (_dsButton[i].isRelease())
                {
                    // do nothing.
                }
                if (_dsButton[i].isDown())
                {
                    // do nothing.
                }
                if (_dsButton[i].isClicked())
                {
                    switch (i)
                    {
                            // Button Go -> go to main game:
                        case 0:
                            {
                                MyGameOwner.MyGameState = new StateMainGame(1);
                                MyGameOwner.MyGameState.InnitState(MyGameOwner);
                                break;
                            }
                        case 1:
                            {
                                MyGameOwner.MyGameState = new StateHelp();
                                MyGameOwner.MyGameState.InnitState(MyGameOwner);
                                break;
                            }
                        case 2:
                            {
                                MyGameOwner.MyGameState = new StateAbout();
                                MyGameOwner.MyGameState.InnitState(MyGameOwner);
                                break;
                            }
                        //Button Exit!
                        case 3:
                            {
                                MyGameOwner.Exit();
                                break;
                            }
                        //Button Load Game!
                        case 4:
                            {
                                string _xmlInfo = @"./XML/SaveGame.xml";
                                XmlDocument _doc = new XmlDocument();
                                _doc.Load(_xmlInfo);
                                int _currentGame = int.Parse(_doc.DocumentElement.SelectSingleNode("//CurrentGame").InnerText);

                                MyGameOwner.MyGameState = new StateMainGame(_currentGame);
                                MyGameOwner.MyGameState.InnitState(MyGameOwner);
                                break;
                            }
                    }
                }
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch sb)
        {
            _backGround.Draw(gameTime, sb);
            for (int i = 0; i < _dsButton.Count; i++)
            {
                _dsButton[i].Draw(gameTime, sb);
            }
        }
    }
}
